import java.util.Arrays;
import java.util.PriorityQueue;


public class TestHeap {
    private int[] elem;
    private int usedSize;
    private Object[] queue;

    public TestHeap(){
        this.elem = new int[10];
    }

    public void initHeap (int[] array){
        for(int i = 0;i < array.length; i++){
            elem[i] = array[i];
            usedSize++;
        }
    }
    public void createHeap(){
        //最后一个子树的父节点
        for(int parent = (usedSize-1-1)/2; parent >= 0 ; parent--){
            shiftDown(parent,usedSize);//向下调整
        }
    }
    //向下调整
    public void shiftDown(int parent, int usedSize){
        int child = (2*parent)+1; //左孩子下标
        while(child < usedSize){
            if(child+1 < usedSize && elem[child] < elem[child+1]){
                child++;//保证child 一定是左右孩子最大值的下标
            }
            if(elem[child]>elem[parent]) {
                swap(child, parent);
                parent = child;
                child = 2 * parent + 1;
            }else{
                break;
            }
        }
    }
    private void swap(int i,int j){
        int tmp = elem[i];
        elem[i] = elem[j];
        elem[j] = tmp;
    }


    //堆的插入 对的插入都是在最后插入 再调整
    public void offer(int val){
        //先判断堆是否满了 满了就扩容
        if(isFull()){
            this.elem = Arrays.copyOf(elem,2*elem.length);
        }
        this.elem[usedSize] = val;
        shiftUp(usedSize);
        usedSize++;
    }
    //向上调整
    private void shiftUp(int child) {
        int parent = (child - 1) / 2;
        while (child > 0) {
            if (elem[child] > elem[parent]) {
                swap(child, parent);
                child = parent;
                parent = (child - 1) / 2;
            } else {
                break;
            }
        }
    }
    public boolean isFull(){
        return usedSize == elem.length;
    }


    //删除 堆的删除一定删除的是堆顶元素
    // 1. 将最后一个元素与对顶元素互换
    // 2. 将堆中的有效数据减一
    // 3. 堆顶元素进行向下调整
    public int poll(){
        int tmp = elem[0];
        swap(0,usedSize-1);
        usedSize--;
        shiftDown(0,usedSize);
        return tmp;
    }

    //扩容
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
    private void grow(int minCapacity) {
        int oldCapacity = queue.length;
        // Double size if small; else grow by 50%
        int newCapacity = oldCapacity + ((oldCapacity < 64) ?
                (oldCapacity + 2) :
                (oldCapacity >> 1)); // overflow-conscious code
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            newCapacity = hugeCapacity(minCapacity);
        queue = Arrays.copyOf(queue, newCapacity);
    }
    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
        throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE; }



    //堆排序
    // 1. 调整为大根堆
    // 2. 让第一个元素和最后一个未排序的元素进行互换
    public void heapSort(){
        int end = usedSize - 1;
        while(end > 0){
            swap(0,end);
            shiftDown(0,end);//调整为大根堆
            end--;
        }
    }

}
